#!/usr/bin/env bash

set -x

(
	cd "$(dirname "$0")"

  ubuntu="${1:-22.04}"
  image="${2}"
  force="$3"

  VOLUME="volume_cuda_$ubuntu"

  if [ ! -e "${VOLUME}/home" ];then
    mkdir -p ${VOLUME}/home
  fi

  if [ ! -e "${VOLUME}/data" ];then
    mkdir -p ${VOLUME}/data
  fi


  container_name="pytorch_with_rdp"
  if [ "$force" == "force" ];then
    (
    docker stop "$container_name"
    docker rm "$container_name"
    ) 2>/dev/null
  fi

  # time zone
  container_args=""
  for f in timezone localtime ;do
    if [ -e "/etc/$f" ];then
      cp "/etc/$f" ${VOLUME}/
    fi
    container_args="$container_args -v $(pwd)/${VOLUME}/$f:/etc/$f"
  done

  # --hostname="$(hostname)" \

  #  --shm-size="1g" \



  docker run -d \
    --restart=always \
    --name="$container_name" \
    --gpus all \
    --hostname="pytorch_xrdp" \
    -p 3389:3389 \
    -p 22022:22 \
    -v "$(pwd)/${VOLUME}/home:/home" \
    -v "$(pwd)/${VOLUME}/data:/data" $container_args \
    -v "/mnt/data/shared/:/data/shared" \
    $image

)


